package com.bazhua.bazo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bazhua.bazo.repository.entity.CheckUser;
import com.bazhua.bazo.repository.entity.CouponCheckRecord;
import com.bazhua.bazo.repository.entity.ShopInfo;
import com.bazhua.bazo.repository.entity.WechatUser;
import com.bazhua.bazo.repository.mapper.CheckUserMapper;
import com.bazhua.bazo.repository.mapper.CouponCheckRecordMapper;
import com.bazhua.bazo.repository.model.CheckUserModel;
import com.bazhua.bazo.repository.model.CouponCheckModel;
import com.bazhua.bazo.repository.model.UserType;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.repository.model.result.ResultUtils;
import com.bazhua.bazo.service.ICheckUserService;
import com.bazhua.bazo.service.ICouponCheckRecordService;
import com.bazhua.bazo.service.IShopInfoService;
import com.bazhua.bazo.service.IWechatUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2021-03-02 0:31
 **/
@Service
public class CheckUserServiceImpl extends ServiceImpl<CheckUserMapper, CheckUser> implements ICheckUserService {

    @Autowired
    private IWechatUserService wechatUserService;

    @Autowired
    private IShopInfoService shopInfoService;

    private ShopInfo getShopByUserId(Long shopUserId) {
        QueryWrapper<ShopInfo> shopInfoQueryWrapper = new QueryWrapper<>();
        shopInfoQueryWrapper.eq("shop_admin", shopUserId);
        shopInfoQueryWrapper.eq("shop_status", 2);
        ShopInfo shop = shopInfoService.getOne(shopInfoQueryWrapper);
        return shop;
    }

    public CommonResult registerCheckUser(String openId, Long shopId) {
        WechatUser wechatUser = wechatUserService.findByOpenId(openId);
        if (wechatUser == null){
            return ResultUtils.error(90200, "用户不存在！");
        }
        if (wechatUser.getUserType() != UserType.User){
            return ResultUtils.error(90200, "非普通用户不能成为核销员！");
        }
        registerCheckUser(wechatUser.getId(), shopId);
        wechatUser.setUserType(UserType.Shop_Staff);
        wechatUserService.updateById(wechatUser);
        return ResultUtils.ok();
    }

    public CheckUser getCheckUserByUserId(Long userId) {
        QueryWrapper<CheckUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        return baseMapper.selectOne(queryWrapper);
    }

    public void registerCheckUser(Long userId, Long shopId) {
        CheckUser checkUser = new CheckUser();
        checkUser.setShopId(shopId);
        checkUser.setUserId(userId);
        checkUser.setCreateTime(new Date());
        baseMapper.insert(checkUser);
    }

    public PageListResult getPage(Integer pageNum, Integer pageSize, Long userId) {
        QueryWrapper<CheckUser> queryWrapper = new QueryWrapper();
        ShopInfo shopInfo = getShopByUserId(userId);
        WechatUser wechatUser = wechatUserService.findById(userId);
        if (wechatUser != null && wechatUser.getUserType() == UserType.Shop_Staff) {
            queryWrapper.eq("user_id", wechatUser.getId());
        }
        if (shopInfo == null) {
            return ResultUtils.pageError(90200, "店铺未审核或已禁用");
        }
        Page<CheckUser> page = new Page<>(pageNum, pageSize);
        page = baseMapper.selectPage(page, queryWrapper);
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            Page<CheckUserModel> modelPage = new Page<>();
            modelPage.setCurrent(page.getCurrent());
            modelPage.setSize(page.getSize());
            modelPage.setTotal(page.getTotal());
            modelPage.setPages(page.getPages());
            modelPage.setMaxLimit(page.getMaxLimit());
            List<CheckUserModel> list = new ArrayList<>();
            for (CheckUser checkUser : page.getRecords()) {
                CheckUserModel checkUserModel = new CheckUserModel();
                WechatUser user = wechatUserService.findById(checkUser.getUserId());
                if (user == null)
                    continue;
                checkUserModel.setId(checkUser.getId());
                checkUserModel.setUserName(user.getUserName());
                list.add(checkUserModel);
            }
            modelPage.setRecords(list);
            return ResultUtils.pageOk(modelPage);
        }
        return ResultUtils.pageError(0, "查询数据为空");
    }

    public CommonResult deleteCheckUser(Long id) {
        baseMapper.deleteById(id);
        return ResultUtils.ok();
    }
}
